package com.htt.app.cuasotinhyeu.server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.htt.app.cuasotinhyeu.utils.Log;

public class Server {
	private static final String TAG = "Server";
	private String data = "";
	
	private InputStream is = null;
	
	public Server(String url) {
		try {
			Log.i(TAG, url);
			
			HttpParams httpParameters = new BasicHttpParams();
			// Set the timeout in milliseconds until a connection is established.
			// The default value is zero, that means the timeout is not used. 
			int timeoutConnection = 3000;
			HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
			// Set the default socket timeout (SO_TIMEOUT) 
			// in milliseconds which is the timeout for waiting for data.
			int timeoutSocket = 5000;
			HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

			DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
			HttpGet httpGet = new HttpGet(url);
			
			HttpResponse httpResponse = httpClient.execute(httpGet);
			
			
			StatusLine statusLine = httpResponse.getStatusLine();
		    int statusCode = statusLine.getStatusCode();
		    
		    if (statusCode == 200) {
		    	HttpEntity httpEntity = httpResponse.getEntity();
		    	is = httpEntity.getContent();
		    	
		    	BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"), 8192);
				StringBuilder sb = new StringBuilder();
				String line = null;
				while((line = reader.readLine()) != null) {
					sb.append(line + "\n");
				}
				
				is.close();
				data = sb.toString();
				Log.i(TAG, data);
		    } else {
		    	Log.w(TAG, "get data fail");
		    }
			 
		} catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } 
	}
	
	public String getData() {
		return data;
	}
	
	public JSONObject getJsonObject() {
		try {
			JSONObject jsonObject = new JSONObject(data);
			return jsonObject;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	public JSONArray getJsonArray() {
		try {
			JSONArray jsonArray = new JSONArray(data);
			return jsonArray;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
}
